
<%@ include file="/common/taglibs.jsp"%>
<!--分页查询共用的页面-->
<%@ include file="/common/common.jsp"%>
<%@ page language="java" pageEncoding="UTF-8"%>
	
<!--引用的标签页js库-->
<script type="text/javascript" src="<%=jsPath%>/jquery/jquery-asTabs.min.js"></script>
<link rel="stylesheet" type="text/css" href="<%=jsPath%>/jquery/css/jquery-asTabs.css" />

<script type="text/javascript" src="<%=jsPath%>/jquery/jquery.timers.js"></script><!--定时器-->
<script type="text/javascript" src="<%=jsPath%>/terminalCommand.js"></script><!--终端命令结果查询-->
<script>


var timerName = "timerForQuery";
var operation = "";//操作类型是查询query还是修改modify
var times=30;
function queryPhotoConfig()
{
    var url = "<%=ApplicationPath%>/terminalParam/send.action";
	var params = {paramId:"0x64", vehicleId:${vehicleId},operation:"query"};
	$.getJSON(url, params, function(result)
	{
		if(result.success)
		{
			var commandId = result.data;
			startQueryResult(commandId);
		}else
		{
			$(".commandMsg").html("下发失败:"+result.message);
		}
	});
}


 function startQueryResult(commandId)
{ 
	times=60;
	$('#btnQuery').attr("disabled","disabled");
	$("#btnQuery").val("等待终端返回查询结果("+times+" )");
	$('body').oneTime('500ms', timerName,function(){  
		 queryResult(commandId); //查询命令的返回结果
	});
}
//根据下发参数查询命令ID，来查询命令返回的参数结果
function queryResult(commandId)
{	
    times--;
	$('#btnQuery').attr("disabled","disabled");
	$("#btnQuery").val("等待终端返回查询结果("+times+" )");
	

	//alert(times);
       var url = "<%=ApplicationPath%>/takePicture/getPictureConfig.action";
	  //var commandId = 0;
	  var params = {commandId:commandId,vehicleId:${vehicleId}};
	   $.getJSON(url, params, 
			function(result){		
				  if(result.success == true)
				 {
					         $(".commandMsg").html('查询成功');
								//返回的查询结果，填充到input输入框中
							  if(console.log)
								  console.log(result.message);
							  setPhotoConfig(result.message);//获取参数后，填充到表单中
							 $("#btnQuery").val("参数查询");
							$('#btnQuery').attr("disabled",false);		  

					  
					 //停止所有的在$('body')上定时器  
					 $('body').stopTime ();   
					//$('#btnQuery').removeAttr("disabled");
				}else {
					if(times == 1)
				   {
						 $(".commandMsg").html("命令执行超时，没有返回结果,请重新查询");
						 $("#btnQuery").val("参数查询");
							$('#btnQuery').attr("disabled",false);
					    $('body').stopTime ();  
				   }else
					{
					   $(".commandMsg").html(result.message);
					     //最多执行10次
						 $('body').oneTime('900ms', timerName,function(){  
								 queryResult(commandId); //查询命令的返回结果
						});
					}
					
				}
		  }
	  );
}

function setPhotoConfig(strConfig)
{
	var configs = strConfig.split(",");
	for(var m = 0; m < 5;m ++)
	{
		var sn = m+1;
        $("#takePicture"+ sn).attr("checked",configs[m] == "1");
	}
	$("#strTimerOn").val(strConfig.substr(0,9));
    for(var m = 8; m < 13;m ++)
	{
		var sn = m-7;
		$("#upload"+ sn).attr("checked",configs[m] == "1");
	}
    $("#strUploadPic").val(strConfig.substr(16,9));

	$("#intervalOnTimer").val(configs[16]);  //定时间隔
	var intervalType = configs[17]; //定时类型 0代表秒 1代表分钟
    Utility.setComboboxDefaultValue("intervalType", intervalType);
}

//当终端命令执行成功时
function onCommandSuccess(result){		
	var data = result.data;
					 $(".commandMsg").html(data.status);
					 if(data.fileName )
					 {
				          var picturePath = "<%=ApplicationPath%>/vehiclePicture/"; 
						  picturePath += data.fileName;
						 $("#imgPic").attr("src", picturePath);
						 var html = "<a href='"+ picturePath + "'>下载文件</a>";
						 $("#td_ViewFile").html(html);
					 }
}

$().ready(function() {
	$('#commandTab').asTabs({panes_wrap: '.pane1'});
	 $("#entityForm").validate(); //初始化验证信息

	 var defaultRoleId = "";//${roleId}; //取出当前车辆的车牌颜色，默认选中
	//ajax填充下拉框数据 填充监听类型 选项
	 $("#picSize").lookup({category:"Resolution", selectedValue:1});
	 $("#quality").lookup({category:"ImageQuality", selectedValue:5});
	Utility.ajaxSubmitForm("entityForm", {
						 success:function(responseText)
						 {
							   var result = responseText;
							   
							     var saveType = $("#saveType").val();
							   TerminalCommand.messageDiv = $(".commandMsg");
								if (result.success) {
										 var commandId = result.data; //下发成功后，获取到命令Id
									 if(saveType == 0)
									 {
										 var url = "<%=ApplicationPath%>/takePicture/getPictureResult.action";
										 TerminalCommand.times=100;
										 TerminalCommand.startQueryResult(commandId,url,onCommandSuccess);//命令下发成功,根据命令id,开始尝试获取检索结果
									  }else
									 {
									 //如果类型是保存到终端，则不需要检测上传
									    TerminalCommand.startQueryResult(commandId);
									 }
								}
								else {
									$(".commandMsg").html("提交失败! 错误原因：" + (result.message ? result.message : result.Data));
									//$('body').stopTime ();  
								 }
						 }
				 });

	Utility.ajaxSubmitForm("timerForm", {
						  beforeSubmit: function(formData, jqForm, options){
						      /**
							 var strSelectedChannel = $("#strSelectedChannel").val();
							 
							 var isValid = $("#timerForm").valid(); //调用Jquery.validate.js的验证插件来验证Form
							 if(strSelectedChannel == null || strSelectedChannel.length == 0)
							  {
								 $(".picCommandMsg").html("请选择通道");
								 isValid = false;
							  }*/
						      var isValid = true;
							 return isValid;
						},
						 success:function(responseText)
						 {
							   var result = responseText;
								if (result.success) {
										 var commandId = result.data; //下发成功后，获取到命令Id
									 //如果类型是保存到终端，则不需要检测上传
									 TerminalCommand.messageDiv = $(".picCommandMsg");
									    TerminalCommand.startQueryResult(commandId);
								}
								else {
									$(".picCommandMsg").html("提交失败! 错误原因：" + (result.message ? result.message : result.Data));
									//$('body').stopTime ();  
								 }
						 }
				 });

		    $("#btnQuery").click(function()
			 {
				  queryPhotoConfig();//查询定时拍照的终端配置
			 });
	
	$(".advanced").hide();
	$('input[name="selectedChannel"]').click(function()
	{
		  var strSelectedChannel = "";
		  $('input[name="selectedChannel"]').each(function()
		  {
			  if(strSelectedChannel.length > 0)
				  strSelectedChannel += ",";
			  strSelectedChannel += this.checked ? 1:0;
		  });
		  $("#strSelectedChannel").val(strSelectedChannel);
	});
	  $('input[name="takePictureOnTimer"]').click(function()
	{
		  var strTimerOn = "";
		  $('input[name="takePictureOnTimer"]').each(function()
		  {
			  if(strTimerOn.length > 0)
				  strTimerOn += ",";
			  strTimerOn += this.checked ? 1:0;
		  });
		  $("#strTimerOn").val(strTimerOn);
	});

     $('input[name="uploadPicture"]').click(function()
	{
		  var strUploadPic = "";
		  $('input[name="uploadPicture"]').each(function()
		  {
			  if(strUploadPic.length > 0)
				  strUploadPic += ",";
			  strUploadPic += this.checked ? 1:0;
		  });
		  $("#strUploadPic").val(strUploadPic);
	});

});
var hide = true;
function showAdvanced()
{
	if(hide)
	{
		$(".advanced").show();
	}else
		$(".advanced").hide();
	hide = hide != true;
}

 
</script>
 <BODY>
 <ul id="commandTab" class="asTabs">
					<li>立即拍照</li>
					<li>定时拍照</li>
				</ul>
				<div class="pane1">
					<div>
	<form id="entityForm" name="entityForm" 
			action='<%=ApplicationPath%>/takePicture/send.action' method="POST">
				
        <input type="hidden"  name="vehicleId"  id="vehicleId" value="${vehicleId}"/>
        <input type="hidden"  name="alarmId"  id="alarmId" value="${alarmId}"/><!--报警处理时，需要把报警ID带给后台-->
  <table width="100%"  class="TableBlock">
					<tbody><tr>
						<td colspan="4" style="font-weight: bold; background: #EFEFEF;" height="25">拍照信息
						<input type="submit" class="sendjson" value="发送命令">
						<span class="commandMsg"></span>
						</td>
					</tr>
					<tr>
						<td align="right">拍摄方式:</td>
						<td >
						    <select id="action" name="action">
						    <option value="1">拍照</option>
						    <option value="2">连拍</option>
						    <option value="3">录像</option>
						    <option value="0">停止</option>
							</select>
						</td>
						<td align="right">摄像头:</td>
						<td >
						    <select id="channel" name="channel">
						    <option value="1">通道1</option>
						    <option value="2">通道2</option>
						    <option value="3">通道3</option>
						    <option value="4">通道4</option>
						    <option value="5">通道5</option>
						    <option value="6">通道6</option>
						    <option value="7">通道7</option>
						    <option value="8">通道8</option>
							</select>
						</td>
					</tr>
					
					<tr>
						
						<td align="right">拍摄张数
							:</td>
						<td>
						<input type="text" id="photoNum"   name="photoNum" maxlength="16" size="20"  value="10"
						   class="required digits min:1" style="width:80px">
						  </td>
						<td align="right">拍摄间隔(分钟):</td>
						<td ><input id="interval" name="interval" class="required digits"  value="10" maxlength="16" size="20" style="width:100px"  class="required digits">
						</td>
					</tr>
					
					<tr>
						<td align="right">保存方式:</td>
						<td >
						 <select id="saveType" name="saveType" class="required">
						    <option value="0">实时上传</option>
						    <option value="1">保存本地</option>
						 </select>
						</td>
						<td align="right">图片尺寸:</td>
						<td >
						   <select id="picSize" name="picSize" class="required" style="width:100px"></select>
						   <a href="#" onclick="showAdvanced();" style="color:#660000">高级设置</a>
						</td>

					</tr>
					
					<tr class="advanced">
						
						<td align="right">品质(1-10):</td>
						<td >
						    <select id="quality" name="quality" class="required"></select>
						</td>
						<td align="right">亮度(0～255):</td>
						<td ><input id="light" name="light" class="required    {required:true,digits:true,range:[0,255]}"  value="125" maxlength="16" size="20" style="width:80px" >
						</td>
					</tr>
				
					<tr  class="advanced">
						<td align="right">对比度(0~127):</td>
						<td ><input id="compare" name="compare" class="required   {required:true,digits:true,range:[0,127]}"  value="125" maxlength="16" size="20" style="width:80px">
						</td>
						<td align="right">饱和度(0~127):</td>
						<td ><input id="stature" name="stature" class="required   {required:true,digits:true,range:[0,127]}"  value="125" maxlength="16" size="20" style="width:80px">
						</td>
					</tr>
					
					<tr  class="advanced">
						<td align="right">色度(0~255):</td>
						<td ><input id="grade" name="grade" class="required   {required:true,digits:true,range:[0,255]}"  value="125" maxlength="16" size="20" style="width:80px">
						</td>
						
					</tr>
					
					<tr>
					   <td id="td_ViewFile"colspan="4" style="font-weight: bold; background: #EFEFEF;" height="25">
										</td>						
						
					</tr>

					<tr>

						<td colspan=4 align="center">
						   <img id="imgPic" src="" style="height:240px;width:500px;"/>
						</td>
						
					</tr>		
					
				</tbody></table>
				</form>
				</div>
				<div><!--定时拍照信息-->
				  <form id="timerForm" name="timerForm" 
			action='<%=ApplicationPath%>/takePicture/pictureOnTimer.action' method="POST">
				
        <input type="hidden"  name="operation"  id="operation" value="modify"/>
        <input type="hidden"  name="strSelectedChannel"  id="strSelectedChannel" value=""/>
        <input type="hidden"  name="strTimerOn"  id="strTimerOn" value="0,0,0,0,0"/>
        <input type="hidden"  name="strUploadPic"  id="strUploadPic" value="0,0,0,0,0"/>
        <input type="hidden"  name="vehicleId"  id="vehicleId" value="${vehicleId}"/>
        <input type="hidden"  name="alarmId"  id="alarmId" value="${alarmId}"/><!--报警处理时，需要把报警ID带给后台-->
      <table width="100%"  class="TableBlock">
					<tbody><tr>
						<td colspan="4" style="font-weight: bold; background: #EFEFEF;" height="25">定时拍照参数设置信息
						<input type="submit" class="sendjson" value="发送定时拍照命令">
						<input type="checkbox" name="allSelectedVehicle" id="allSelectedVehicle" value="on"/>设置所有选中的车辆
						
						<input type="button" name="query"  id="btnQuery"  value="参数查询" />  
						<span class="picCommandMsg"></span>
						</td>
					</tr>
					    <tr>
						
						<td align="right">拍摄间隔:</td>
						<td ><input id="intervalOnTimer" name="interval" class="required digits"  value="60" maxlength="16" size="20" style="width:100px"  ><span style="color:red">*</span>
						</td>
						<td align="right">时间单位:</td>
						<td >
						    <select id="intervalType" name="intervalType" class="required">
						      <option value="0">秒(小于5秒按5秒处理)</option>
						      <option value="1">分</option>
						  </select>
						</td>
					</tr>
					<!--
					<tr>
						
						<td align="right">有效状态:</td>
						<td colspan=3> 
						  <select id="saveType" name="saveType" class="required">
						    <option value="0">一直有效</option>
						  </select>
						</td>
					</tr>
					-->
					<tr>
						
						<td align="left" colspan=4>
						   第一路
						   <input type="checkbox" name="takePictureOnTimer" value="true" id="takePicture1"  />打开定时拍照 
						   <input type="checkbox" name="uploadPicture" value="true" id="upload1"  />上传照片 
						</td>
					</tr>
					<tr>
						
						<td align="left" colspan=4>
						   第二路
						   <input type="checkbox" name="takePictureOnTimer" value="true" id="takePicture2"  />打开定时拍照 
						   <input type="checkbox" name="uploadPicture" value="true" id="upload2"  />上传照片 
						</td>
					</tr>
					<tr>
						
						<td align="left" colspan=4>
						   第三路
						   <input type="checkbox" name="takePictureOnTimer" value="true" id="takePicture3"  />打开定时拍照 
						   <input type="checkbox" name="uploadPicture" value="true" id="upload3"  />上传照片 
						</td>
					</tr>
					<tr>
						
						<td align="left" colspan=4>
						   第四路
						   <input type="checkbox" name="takePictureOnTimer" value="true" id="takePicture4"  />打开定时拍照 
						   <input type="checkbox" name="uploadPicture" value="true" id="upload4"  />上传照片 
						</td>
					</tr>
					<tr>
						
						<td align="left" colspan=4>
						   第五路
						   <input type="checkbox" name="takePictureOnTimer" value="true" id="takePicture5"  />打开定时拍照 
						   <input type="checkbox" name="uploadPicture" value="true" id="upload5"  />上传照片 
						</td>
					</tr>
					</table>
					    </form>
				</div>
			</div>
 </BODY>
</HTML>
